package com.woniu.adopt.web;

import com.github.pagehelper.PageInfo;
import com.woniu.adopt.entity.Activity;
import com.woniu.adopt.result.ResultResponse;
import com.woniu.adopt.service.ActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("activity")
@Api(description = "团队活动管理接口")
public class ActivityController {

    @Autowired
    ActivityService activityService;


    //模糊查询 （团队活动名称，发布状态）+ 分页
    @GetMapping("/listActivity")
    @ApiOperation(value = "本接口按条件分页查询团队活动数据")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页", dataType = "int", paramType = "query", name = "now"),
            @ApiImplicitParam(value = "分页单位", dataType = "int", paramType = "query", name = "size"),
            @ApiImplicitParam(value = "按照团队活动名称条件查询", dataType = "String", paramType = "query", name = "name"),
            @ApiImplicitParam(value = "按照团队活动名称条件查询", dataType = "String", paramType = "query", name = "address"),
            @ApiImplicitParam(value = "按照发布状态模糊查询条件", dataType = "int", paramType = "query", name = "display")
    })
    public ResultResponse<PageInfo<Activity>> list(
            @RequestParam(value = "now", defaultValue = "1") Integer now,
            @RequestParam(value = "size", defaultValue = "3") Integer size,
            String name, String address,
            @RequestParam(value = "display", defaultValue = "") int display) {
        ResultResponse<PageInfo<Activity>> result = null;
        try {
            PageInfo<Activity> pageInfo = activityService.queryActivityByPage(now, size, name, address, display);
            result = new ResultResponse<>(200, "查询团队活动成功!", pageInfo);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询团队活动失败!");
        }
        return result;
    }


    //模糊查询 （团队活动名称）+ 分页
    @GetMapping("/listOldActivity")
    @ApiOperation(value = "本接口按条件分页查询团队活动数据")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页", dataType = "int", paramType = "query", name = "now"),
            @ApiImplicitParam(value = "分页单位", dataType = "int", paramType = "query", name = "size"),
            @ApiImplicitParam(value = "按照团队活动名称条件查询", dataType = "String", paramType = "query", name = "name"),
            @ApiImplicitParam(value = "按照团队活动名称条件查询", dataType = "String", paramType = "query", name = "address"),
            @ApiImplicitParam(value = "按照发布状态模糊查询条件", dataType = "int", paramType = "query", name = "display")
    })
    public ResultResponse<PageInfo<Activity>> oldList(
            @RequestParam(value = "now", defaultValue = "1") Integer now,
            @RequestParam(value = "size", defaultValue = "3") Integer size,
            String name, String address,
            @RequestParam(value = "display", defaultValue = "") int display) {

        ResultResponse<PageInfo<Activity>> result = null;

        try {
            PageInfo<Activity> pageInfo = activityService.queryOldActivityByPage(now, size, name, address, display);
            result = new ResultResponse<>(200, "查询历史团队活动成功!", pageInfo);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询历史团队活动失败!");
        }
        return result;
    }


    @PostMapping("/addActivity")  //添加用  PostMapping
    @ApiOperation(value = "添加团队活动")
    public ResultResponse<Void> addRS(@RequestBody Activity activity) {
        ResultResponse<Void> result = null;
        try {
            activity.setDel(0);
            activity.setInsertTime(new Date());
            int i = activityService.insertActivity(activity);
            if (i == 1) {
                result = new ResultResponse<>(200, "活动添加成功！");
            } else {
                SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                //日期时间转换
                Date d1 = activity.getInsertTime();
                result = new ResultResponse<>(201, "活动添加失败，活动开始时间不能早于" + sim.format(d1) + "！！！");
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(203, "系统故障，请联系管理员！！！");
        }
        return result;
    }

    //修改返显
    @GetMapping("/getActivityByKey/{id}")
    @ApiOperation(value = "本接口按id查询团队活动数据")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "按照id查询救助站", dataType = "int", paramType = "query", name = "id")
    })
    public ResultResponse<Activity> getActivityByKey(@PathVariable("id") int id) {

        ResultResponse<Activity> result = null;
        try {
            Activity activity = activityService.selectActivityByKey(id);
            result = new ResultResponse<>(200, "查询团队活动成功!", activity);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询团队活动失败!");
        }
        return result;
    }

    @PutMapping("/modify")
    @ApiOperation(value = "修改活动的信息")
    public ResultResponse<Void> modify(@RequestBody Activity activity) {
        ResultResponse<Void> result = null;
        try {
            activity.setUpdateTime(new Date());
            int i = activityService.updateActivity(activity);
            if (i == 1) {
                result = new ResultResponse<>(200, "活动修改成功！");
            } else {
                SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                //日期时间转换
                Date d1 = activity.getUpdateTime();
                result = new ResultResponse<>(201, "活动修改失败，活动开始时间不能早于" + sim.format(d1) + "！！！");
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(203, "系统故障，请联系管理员！！！");
        }
        return result;
    }


    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据团队活动的标识ID 逻辑删除活动")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "id", value = "团队活动的ID标识")
    })
    public ResultResponse<Void> del(@PathVariable("id") int id) {
        ResultResponse<Void> result = null;

        try {
            activityService.removeActivity(id);
            result = new ResultResponse<>(200, "删除活动成功!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(203, "删除活动失败!");
        }
        return result;
    }


    @DeleteMapping("/trueDel/{id}")
    @ApiOperation(value = "根据团队活动的标识ID 真删，数据库也将删除这条数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "id", value = "团队活动的ID标识")
    })
    public ResultResponse<Void> trueDel(@PathVariable("id") int id) {
        ResultResponse<Void> result = null;

        try {
            activityService.removeActivity2(id);
            result = new ResultResponse<>(200, "删除活动成功!");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(203, "删除活动失败!");
        }
        return result;
    }


}
